﻿using System;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Facade.ParvisService;
using System.Collections.Generic;
using StockholmsStad.Windows.UI.Facade.CeremonyService;


namespace StockholmsStad.Windows.UI.Facade
{
    public class CeremonyFacade : FacadeBase
    {
        #region constructor
        public CeremonyFacade(User user) : base(user)
        {
        }
        #endregion

        #region Public methods

        public List<ValueIdPair> GetCeremonyTypes()
        {
            var client = new WinServiceClient();

            try
            {
                var list = new List<ValueIdPair>();
                var cerTypes = client.GetCeremonyTypes(CurrentUser);
                client.Close();
                list.AddRange(cerTypes);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<ValueIdPair> GetCeremonyLanguages()
        {
            var client = new WinServiceClient();

            try
            {
                var languages = new List<ValueIdPair>();
                var langs = client.GetCeremonyLanguages(CurrentUser);
                client.Close();
                languages.AddRange(langs);
                return languages;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }


        public List<ValueIdPair> GetCertificateLanguages()
        {
            var client = new WinServiceClient();

            try
            {
                var languages = new List<ValueIdPair>();
                var langs = client.GetCertificateLanguages(CurrentUser);
                client.Close();
                languages.AddRange(langs);
                return languages;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public ValueIdPair GetDefaultLanguage()
        {
            var client = new WinServiceClient();

            try
            {
                var lang = client.GetDefaultLanguage(CurrentUser);
                client.Close();
                return lang;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<ValueIdPair> GetSpecialNeeds()
        {
            var client = new WinServiceClient();

            try
            {
                var needs = new List<ValueIdPair>();
                var specialNeeds = client.GetCeremonySpecialNeeds(CurrentUser);
                client.Close();
                needs.AddRange(specialNeeds);
                return needs;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public DateTime GetNextCeremonyDateForIndividualService()
        {
            var client = new CeremonyServiceClient();

            try
            {
                var date = client.GetNextCeremonyDateForIndividualService(CurrentUser);
                client.Close();
                return date;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public DateTime GetNextCeremonyDateForOrdinaryService()
        {
            var client = new CeremonyServiceClient();

            try
            {
                var date = client.GetNextCeremonyDateForOrdinaryService(CurrentUser);
                client.Close();
                return date;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public DateTime GetPreviousCeremonyDateForIndividualService()
        {
            var client = new CeremonyServiceClient();

            try
            {
                var date = client.GetPreviousCeremonyDateForIndividualService(CurrentUser);
                client.Close();
                return date;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public DateTime GetPreviousCeremonyDateForOrdinaryService()
        {
            var client = new CeremonyServiceClient();

            try
            {
                var date = client.GetPreviousCeremonyDateForOrdinaryService(CurrentUser);
                client.Close();
                return date;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummary> GetIndividualCeremonies(DateTime date)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummary>();
                var ceremonies = client.GetIndividualCeremonyBookingForDate(CurrentUser, date);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummary> GetIndividualCeremonies(DateTime fromDate, DateTime? toDate, int? ministerId)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummary>();
                var ceremonies = client.GetIndividualCeremonyBooking(CurrentUser, fromDate, toDate, ministerId);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummaryDetails> GetIndividualCeremonyDetails(DateTime date, int? ministerId)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummaryDetails>();
                var ceremonies = client.GetIndividualCeremonyBookingDetails(CurrentUser, date, ministerId);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummary> GetOrdinaryCeremonies(DateTime date)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummary>();
                var ceremonies = client.GetOrdinaryCeremonyBookingForDate(CurrentUser, date);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummary> GetOrdinaryCeremonies(DateTime fromDate, DateTime? toDate, int? ministerId)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummary>();
                var ceremonies = client.GetOrdinaryCeremonyBooking(CurrentUser, fromDate, toDate, ministerId);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<CeremonySummaryDetails> GetOrdinaryCeremonyDetails(DateTime date)
        {
            var client = new CeremonyServiceClient();
            try
            {
                var list = new List<CeremonySummaryDetails>();
                var ceremonies = client.GetOrdinaryCeremonyBookingDetails(CurrentUser, date);
                client.Close();
                list.AddRange(ceremonies);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        #endregion
    }
}
